package com.example.cet4try.diyView;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.cet4try.R;

public class TopView extends RelativeLayout{

    private Context mContext;
    private Dialog mDialog;
    private TextView tv_title;
    private Button btn_exit;
    private boolean hexit = true;

    public TopView(Context context) {
        super(context);
    }

    public void setmContext(Context context) {
        this.mContext = context;
    }

    public void setmDialog(Dialog mDialog) {
        this.mDialog = mDialog;
    }

    public void setmTitle(String title){
        tv_title.setText(title);
    }

    public void setHaveExit(boolean hexit) {
        if (hexit){
            btn_exit.setVisibility(VISIBLE);
        }
        else
            btn_exit.setVisibility(INVISIBLE);
    }

    public TopView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;

        LayoutInflater.from(context).inflate(R.layout.toptab_view_layout, this);

        TypedArray tarr = context.obtainStyledAttributes(attrs,R.styleable.TopView);
        String title = tarr.getString(R.styleable.TopView_Title);
        tv_title = findViewById(R.id.title);
        tv_title.setText(title);

        btn_exit = findViewById(R.id.exit);
//        if (hexit){
        btn_exit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mDialog != null){
                    mDialog.dismiss();
                }else if (mContext != null){
                    Activity activity = (Activity) mContext;
                    activity.finish();
                }

            }
        });
//        }
//        else

        boolean habout = tarr.getBoolean(R.styleable.TopView_About,false);
        if (habout){
            Button btn_about = findViewById(R.id.about);
            btn_about.setVisibility(VISIBLE);

        }


    }



}
